package com.ielts.activity;

import java.util.ArrayList;
import java.util.Calendar;

import vn.com.baseapplication.BaseFragment;
import vn.com.baseapplication.constant.BaseConstant;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.ielts.IeltsConstant;
import com.ielts.activity.adapter.MainAdapter;
import com.ielts.fragment.AllFragment;
import com.ielts.fragment.BuyedFragment;
import com.ielts.model.ItemTopics;
import com.ielts.services.IeltsServices;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends IeltsActivity {
	public static final String TAG = MainActivity.class.getName();

	IeltsServices mServices;
	MainAdapter adapter;
	ViewPager pager;
	TabPageIndicator indicator;
	public ArrayList<ItemTopics> listIelts = new ArrayList<>();

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e("Broadcast", "OnReceive");
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().hide();
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {

		}

		adapter = new MainAdapter(getSupportFragmentManager());

		pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(adapter);
		pager.setOffscreenPageLimit(5);

		indicator = (TabPageIndicator) findViewById(R.id.indicator);
		indicator.setHorizontalFadingEdgeEnabled(true);
		// indicator.setPadding(0, 22, 0, 0);
		indicator.setViewPager(pager);
		// indicator.setBackgroundResource(R.drawable.tabbar_bg);

		indicator.setBackgroundResource(R.drawable.blue_default);

	}

	private static long back_pressed;

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis())
			super.onBackPressed();
		else
			Toast.makeText(getBaseContext(), getString(R.string.back_again), Toast.LENGTH_SHORT).show();
		back_pressed = System.currentTimeMillis();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			// do nothing
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!isServiceConnected()) {
			bindBaseService(IeltsServices.class);
		} else {

		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		for (int index = 0; index < adapter.getCount(); index++) {
			BaseFragment fragment = (BaseFragment) adapter.getItem(index);
			fragment.onReceive(context, intent);
		}

		if (!intent.hasExtra(BaseConstant.MSG_FLAG)) {
			return;
		}
		String FLAG = intent.getStringExtra(BaseConstant.MSG_FLAG) + "";
		if (FLAG != null && (FLAG + "").equals(IeltsConstant.REGISTER_USER+"")) {
			mServices.getUserBalance();
		}
	}

	@Override
	public void onServiceConnected() {
		super.onServiceConnected();
		for (int index = 0; index < adapter.getCount(); index++) {
			BaseFragment fragment = (BaseFragment) adapter.getItem(index);
			fragment.onServiceConnected();
		}
		mServices = (IeltsServices) getBaseServices();
		Log.e(TAG, mServices.getHelloString());
		if (IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_ID, IeltsConstant.KEY_USER_ID_DEF).equals(IeltsConstant.KEY_USER_ID_DEF)) {
			mServices.registerUserPost();
		} else
			mServices.getUserBalance();

	}

	@Override
	public void onNetSuccess(int requestId, String response) {
		super.onNetSuccess(requestId, response);
		setLoading(false);
	}

	@Override
	public void onNetSuccessCache(int requestId, String response) {
		super.onNetSuccessCache(requestId, response);
		setLoading(false);
	}

	@Override
	public void onNetFailed(int requestId, String error) {
		super.onNetFailed(requestId, error);
		setLoading(false);
	}

	@Override
	public void onNetTimeOut(int requestId, Throwable error) {
		super.onNetTimeOut(requestId, error);
		setLoading(false);
	}

	@Override
	public void onNetLoading(int requestId) {
		super.onNetLoading(requestId);
		setLoading(true);
	}

	public void setLoading(final boolean isLoading) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (isLoading) {

				} else {

				}
			}
		});
	}

	public AlertDialog.Builder setupSelectYearDialog() {
		AlertDialog.Builder builderSingle = new AlertDialog.Builder(
				MainActivity.this);
		builderSingle.setIcon(R.drawable.ic_launcher);
		builderSingle.setTitle("Select Year:");
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
				MainActivity.this,
				android.R.layout.select_dialog_singlechoice);
		
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int currentYear = Integer.valueOf(IeltsApplication.getInstance().getValue(IeltsConstant.KEY_YEAR, year+""));
		int currentYearIdx = 0;
		for (int i = year; i >= 2013; i--) {
			arrayAdapter.add(i + "");
			if(i==currentYear)currentYearIdx=year-i;
		}
		builderSingle.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builderSingle.setSingleChoiceItems(arrayAdapter, currentYearIdx, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String strName = arrayAdapter.getItem(which);
				IeltsApplication.getInstance().setValue(IeltsConstant.KEY_YEAR, strName);
				((AllFragment) MainActivity.this.adapter.allFrgmt).getAllTopics();
				((BuyedFragment) MainActivity.this.adapter.buyedFrgmt).getBoughtTopics();
				dialog.dismiss();
			}
		});
		return builderSingle;
	}
}
